<script setup lang="ts">
import { getHomeBannerAPI,getHomeCategoryAPI,getHomeHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import CustomNavbar from './components/CustomNavbar.vue'
import { ref } from 'vue';
import type { BannerItem,CategoryItem,HotItem } from '@/types/home';
import CategoryPanel from './components/CategoryPanel.vue';
import HotPanel from './components/HotPanel.vue';
import pageSkeleton from '@/pages/index/components/pageSkeleton.vue'
import {guessList} from '@/composables/index'
// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerDate = async () => {
  const res = await getHomeBannerAPI()
  console.log(res)
  bannerList.value = res.result
}
// 获取前台分类数据
const getCategoryList = ref<CategoryItem[]>([])
const getHomeCategoryDate = async () => {
  const res = await getHomeCategoryAPI()
  getCategoryList.value = res.result

}
// 获取前台热门推荐数据
const getHomeHotList = ref<HotItem[]>([])
const getHomeHotDate = async () => {
  const res = await getHomeHotAPI()
  getHomeHotList.value = res.result
}
// 定义骨架屏数据
const isLoding = ref(false)
onLoad(async () => {
  isLoding.value = true
  await Promise.all([getHomeBannerDate(),getHomeCategoryDate(),getHomeHotDate()])
  isLoding.value = false
})
// 获取猜你喜欢组件实例
const {guessRef,onScrolltolower} = guessList() 
const isTriggered = ref(false)
// 自定义下拉刷新栏触发
const onRefresherrefresh = async () => {
  isTriggered.value = true
  guessRef.value.restereData()
  await Promise.all([getHomeBannerDate(),getHomeCategoryDate(),getHomeHotDate(),
  guessRef.value?.getMore()])
  isTriggered.value = false
 
}
</script>
 
<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <scroll-view 
  refresher-enabled @refresherrefresh="onRefresherrefresh" :refresher-triggered="isTriggered" 
  @scrolltolower="onScrolltolower" class="scroll-view" scroll-y>
  <pageSkeleton v-if="isLoding"></pageSkeleton>
   <template v-else>
    <!-- 自定义轮播图 -->
    <XtxSwiper :list = "bannerList"/>
    <!-- 前台分类类目 -->
    <CategoryPanel :list = "getCategoryList"/>
    <!-- 前台-热门推荐模块 -->
    <HotPanel :list = "getHomeHotList"/>
    <!-- 猜你喜欢模块 -->
    <XtxGuess ref="guessRef"/>
   </template>
    
  </scroll-view>

</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex:1
}
</style>
